package com.baselib.android.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.baselib.android.utils.ListUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;


/**
 * 所有的 adapter的父类
 */
public abstract class BasePlatAdapter<T, H extends BaseHolder> extends android.widget.BaseAdapter {
    protected Context context;
    protected List<T> listData;
    protected LayoutInflater mLayoutInflater;

    public BasePlatAdapter(Context context) {
        this.context = context;
        listData = new ArrayList<T>();
        mLayoutInflater = LayoutInflater.from(context);

    }

    public void replace(T t, int position) {
        listData.set(position, t);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (listData == null) {
            return 0;
        }
        return listData.size();
    }

    @Override
    public T getItem(int position) {
        if (listData == null) {
            return null;
        }
        try {
            return listData.get(position);
        } catch (Exception e) {
            return null;
        }

    }

    /**
     * 获取集合
     *
     * @return
     */
    public List<T> getItems() {
        return listData;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void add(int location, T data) {
        if (listData.contains(data)) {
            return;
        }
        listData.add(location, data);
        notifyDataSetChanged();
    }

    public void append(T data) {

        listData.add(data);
        notifyDataSetChanged();
    }

    public void appendArray(List<T> array) {
        listData.addAll(array);
        notifyDataSetChanged();
    }


    public void removeByPosition(int position) {
        listData.remove(position);
        notifyDataSetChanged();
    }

    public void setRes(Collection<T> list) {
        if (list == null) {
            return;
        }
        listData.clear();
        listData.addAll(list);
        notifyDataSetChanged();
    }

    public void addRes(List<T> list) {
        if (ListUtil.isNullOrEmpty(list)) {
            return;
        }
        listData.addAll(list);
        notifyDataSetChanged();
    }

    public void addResAtHead(List<T> list) {
        if (ListUtil.isNullOrEmpty(list)) {
            return;
        }
        listData.addAll(0, list);
        notifyDataSetChanged();

    }

    public void addTAtHead(T em) {
        if (em == null) {
            return;
        }
        listData.add(0, em);
        notifyDataSetChanged();

    }

    protected <T extends View> T r2v(View view, int resId) {
        return (T) view.findViewById(resId);
    }

    protected abstract H createHolder(View view);

    protected abstract int layoutId();

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        H holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(layoutId(),
                    parent, false);
            holder = createHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (H) convertView.getTag();
        }

        bindData(getItem(position), holder);
        return convertView;
    }

    protected abstract void bindData(T t, H h);


}
